package kiwi.assist.dev.guieditor.view {
	import org.aswing.ASColor;

	import flash.geom.Rectangle;

	import kiwi.assist.dev.guieditor.model.SkinConfig;

	import com.greensock.layout.AutoFitArea;

	import flash.display.DisplayObject;
	import flash.display.Sprite;

	/**
	 * @author zhangming.luo
	 */
	public class SkinCanvasView extends Sprite {
		private var skinConfig : SkinConfig;
		private var area : AutoFitArea;
		private var skin : DisplayObject;
		private var preview : Sprite;

		public function SkinCanvasView() {
			preview = new Sprite;
			addChild(preview);
			area = new AutoFitArea(preview, 1, 1, 280, 280);
		}

		public function showSkin(skinConfig : SkinConfig) : void {
			clear();
			this.skinConfig = skinConfig;
			if (!skinConfig) {
				return;
			}
			skin = skinConfig.newInstance();
			preview.addChildAt(skin, 0);
			if (skin.scale9Grid) {
				var grid : Sprite = new Sprite;
				// 绘制9grid
				var bounds : Rectangle = skin.getBounds(skin);
				grid.graphics.lineStyle(1, 0xffffff, 0.01, true);
				grid.graphics.drawRect(bounds.x - 1, bounds.y - 1, bounds.width + 2, bounds.height + 2);
				grid.graphics.lineStyle(1, ASColor.PINK.getRGB(), 1);
				grid.graphics.moveTo(bounds.x, skin.scale9Grid.y);
				grid.graphics.lineTo(bounds.right, skin.scale9Grid.y);
				grid.graphics.moveTo(bounds.x, skin.scale9Grid.bottom);
				grid.graphics.lineTo(bounds.right, skin.scale9Grid.bottom);
				grid.graphics.moveTo(skin.scale9Grid.left, bounds.top);
				grid.graphics.lineTo(skin.scale9Grid.left, bounds.bottom);
				grid.graphics.moveTo(skin.scale9Grid.right, bounds.top);
				grid.graphics.lineTo(skin.scale9Grid.right, bounds.bottom);
				preview.addChild(grid);
				area.attach(grid);
			}
			area.attach(skin);
		}

		private function clear() : void {
			while (preview.numChildren) preview.removeChildAt(0);
		}
	}
}
